package synch;

/**
 * A runnable that transfers money from an account to other accounts in a bank.
 * @author andy
 */
public class TransferRunnable implements Runnable {

    private Bank bank;
    private int fromAccount;
    private double maxAmount;
    private int DELAY = 10;

    /**
     * Constructs a transfer runnable.
     * @param b the bank between whose account money is transferred.
     * @param from the account to transfer money from.
     * @param max the maximum amount of money in each transfer.
     */
    public TransferRunnable(Bank bank, int fromAccount, double maxAmount) {
        this.bank = bank;
        this.fromAccount = fromAccount;
        this.maxAmount = maxAmount;
    }

    public void run() {
        try {
            while (true) {
                int toAccount = (int) (this.bank.size() * Math.random());
                double amount = this.maxAmount * Math.random();
                this.bank.transfer(this.fromAccount, toAccount, amount);
                Thread.sleep((int) (DELAY * Math.random()));
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
